DESTDIR:=
VERSION:=$(shell sed '/^__version__/!d; s/^[^'"'"']*'"'"'//; s/'"'"'.*$$//' src/main.py)

.PHONY: all install clean uninstall archive deb upload-archive upload-deb upload

all:

# ---

install: \
  src/*.py \
  GNOME_HabrMonitorApplet.server \
  habr-monitor-applet.png \
  
	mkdir -p -- "$(DESTDIR)"/usr/lib/gnome-panel/habr-monitor-applet
	cp -- src/*.py "$(DESTDIR)"/usr/lib/gnome-panel/habr-monitor-applet

	mkdir -p -- "$(DESTDIR)"/usr/lib/bonobo/servers/
	cp -- GNOME_HabrMonitorApplet.server "$(DESTDIR)"/usr/lib/bonobo/servers/

	mkdir -p -- "$(DESTDIR)"/usr/share/gnome-panel/pixmaps
	cp -- habr-monitor-applet.png "$(DESTDIR)"/usr/share/gnome-panel/pixmaps

	$(MAKE) -C locale/ install

clean:
	$(MAKE) -C locale/ clean

uninstall:
	-rm -r -- "$(DESTDIR)"/usr/lib/gnome-panel/habr-monitor-applet
	-rm -- "$(DESTDIR)"/usr/lib/bonobo/servers/GNOME_HabrMonitorApplet.server
	-rm -- "$(DESTDIR)"/usr/share/gnome-panel/pixmaps/habr-monitor-applet.png
	$(MAKE) -C locale/ uninstall
	
# ---

habr-monitor-applet-${VERSION}.tar.gz:         \
  makefile                                     \
  locale/makefile locale/*.po                  \
  src/*.py                                     \
  GNOME_HabrMonitorApplet.server               \
  habr-monitor-applet.png                      \
  INSTALL                                      \
  
	TEMP=/tmp/$$$$/habr-monitor-applet-"${VERSION}"; \
	mkdir -p "$${TEMP}";                             \
	                                                 \
	mkdir "$${TEMP}"/locale/;                        \
	cp locale/*.po "$${TEMP}"/locale/;               \
	                                                 \
	for file in $^;                                  \
	do                                               \
	  mkdir -p "$${TEMP}"/"$$(dirname "$${file}")";  \
	  cp "$${file}" "$${TEMP}"/"$${file}";           \
	done;                                            \
	                                                 \
	DIR="$${PWD}";                                   \
	cd "$$(dirname "$${TEMP}")";                     \
	tar -czf "$${DIR}"/$@ "$$(basename "$${TEMP}")"; \
	cd "$${DIR}";                                    \
	                                                 \
	rm -r "$${TEMP}"

archive: habr-monitor-applet-${VERSION}.tar.gz

# ---

habr-monitor-applet_${VERSION}-0ubuntu1_all.deb: \
  habr-monitor-applet-${VERSION}.tar.gz \
  deb/debian/* \
  
	cp habr-monitor-applet-"${VERSION}".tar.gz                      \
	  deb/habr-monitor-applet_"${VERSION}".orig.tar.gz;             \
	tar -xz -C deb/ -f habr-monitor-applet-"${VERSION}".tar.gz;     \
	                                                                \
	mkdir deb/habr-monitor-applet-"${VERSION}"/debian/ 2>/dev/null; \
	cp deb/debian/* deb/habr-monitor-applet-"${VERSION}"/debian/;   \
	                                                                \
	cd deb/habr-monitor-applet-"${VERSION}"/;                       \
	debuild;                                                        \
	cd ../../;                                                      \
	cp deb/habr-monitor-applet_"${VERSION}"-0ubuntu1_all.deb .

deb: habr-monitor-applet_${VERSION}-0ubuntu1_all.deb

# ---	

upload-archive: habr-monitor-applet-${VERSION}.tar.gz
	./googlecode-upload.py \
	  -u earshinov \
	  -p habr-monitor-applet \
	  -s Архив \
	  -l 'Type-Archive,OpSys-Linux,Featured' \
	  $^

upload-deb: habr-monitor-applet_${VERSION}-0ubuntu1_all.deb
	./googlecode-upload.py \
	  -u earshinov \
	  -p habr-monitor-applet \
	  -s Deb-пакет \
	  -l 'Type-Package,OpSys-Linux,Featured' \
	  $^
	  
upload: upload-archive upload-deb

